package com.wx.business.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import com.alibaba.fastjson.JSONObject;
import com.wx.model.WtBankCard;


public  class BankCardUtil {
	
	 /**
     * 通过GET方式发起http请求
     */
    public JSONObject requestByGetMethod(String cardNo){
        //创建默认的httpClient实例
        CloseableHttpClient httpClient = getHttpClient();
        JSONObject resultJsonObject=null;
        try {
            //用get方法发送http请求
            HttpGet get = new HttpGet("http://e.apix.cn/apixcredit/bankcardinfo/bankcardinfo?cardno="+cardNo);
            System.out.println("执行get请求:...."+get.getURI());
            get.addHeader("accept", "application/json");
  		    get.addHeader("content-type", "application/json");
  		    get.addHeader("apix-key", "69628ee73bd94aa376d9944bce3b9f55");
            CloseableHttpResponse httpResponse = null;
            
            //发送get请求
            httpResponse = httpClient.execute(get);
            try{
                //response实体
                HttpEntity entity = httpResponse.getEntity();
                StringBuilder entityStringBuilder=new StringBuilder();
    			if (entity!=null) {
    				try {
    					BufferedReader	bufferedReader=new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"), 8*1024);
    				    String line=null;
    					while ((line=bufferedReader.readLine())!=null) {
    						entityStringBuilder.append(line);
    					}
    					//利用从HttpEntity中得到的String生成JsonObject
    					String demo=entityStringBuilder.toString();
    					resultJsonObject=JSONObject.parseObject(demo);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
            }
            }
            finally{
                httpResponse.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally{
            try{
                closeHttpClient(httpClient);
            } catch (IOException e){
                e.printStackTrace();
            }
        }
        
		return resultJsonObject;

 
    }
     
  
     
    private CloseableHttpClient getHttpClient(){
        return HttpClients.createDefault();
    }
     
    private void closeHttpClient(CloseableHttpClient client) throws IOException{
        if (client != null){
            client.close();
        }
    }
	
	public static WtBankCard getBasicBankCard(String cardNo){
		
		JSONObject object=	new BankCardUtil().requestByGetMethod(cardNo);
		JSONObject basicInfo= object.getJSONObject("data");
		if(basicInfo==null)
		{
			return  null;
		}
		WtBankCard	card=new WtBankCard();
		card.setBankBranchName(basicInfo.getString(""));
		card.setBankName(basicInfo.getString("bankname"));
		card.setBankNum(basicInfo.getString("banknum"));
		card.setLogoUrl(basicInfo.getString("logourl"));
		card.setCardPrefixNum(basicInfo.getString("cardprefixnum"));
		card.setCardType(basicInfo.getString("cardtype"));
		card.setCity(basicInfo.getString("city"));
		card.setProvince(basicInfo.getString("province"));
		card.setCardName(basicInfo.getString("cardname"));
		card.setBankTelephone(basicInfo.getString("servicephone"));;
		return card;
	}

}
